package simulation;

import java.util.List;
import logging.LogSetup;
import org.apache.log4j.Logger;
import persistence.DataReader;

/**
 * Loads the car specs by defaults.
 * TODO: This may be be extendible to read from files or some sort of specific configuration
 * @author Danielondon
 */
public class CarSpecsFactory {
    protected static Logger logger = LogSetup.getLogger();
    
    public static ICarSpecs createCarSpecs(String []params) {
        CarSpecs carSpecs = new CarSpecs();
        carSpecs.setBrand(params[0]);
        carSpecs.setModel(params[1]);        
        carSpecs.setMinimumBatteryCharge(Double.parseDouble(params[2]));
        carSpecs.setBatteryCapacity(Double.parseDouble(params[3]));
        carSpecs.setPowerConsumptionPerKilometer(Double.parseDouble(params[4]));
        carSpecs.setPowerConsumptionAtStartup(Double.parseDouble(params[5]));
        return carSpecs;
    }
    
     public static ICarSpecs createCarSpecs(String fileName) {
         CarSpecs carSpecs = null;
         try {

            DataReader dataReader = new DataReader(fileName);
            List<String[]> data = dataReader.getData();
            String [] tokens;
            carSpecs = new CarSpecs();
            int i = 0;
            int INDEX_VALUE = 1;            
            
            tokens = data.get(i++);
            carSpecs.setBrand(tokens[INDEX_VALUE]);
            tokens = data.get(i++);
            carSpecs.setModel(tokens[INDEX_VALUE]);
            tokens = data.get(i++);
            carSpecs.setMinimumBatteryCharge(Double.parseDouble(tokens[INDEX_VALUE]));
            tokens = data.get(i++);
            carSpecs.setBatteryCapacity(Double.parseDouble(tokens[INDEX_VALUE]));
            tokens = data.get(i++);
            carSpecs.setPowerConsumptionPerKilometer(Double.parseDouble(tokens[INDEX_VALUE]));
            tokens = data.get(i++);
            carSpecs.setPowerConsumptionAtStartup(Double.parseDouble(tokens[INDEX_VALUE]));
         } catch (Exception ex) {
             logger.error(ex);
         }
         
         return carSpecs;
     }
	
    public static ICarSpecs getDefaultChevroletSpecs() {
        return new ChevroletCarSpecs();
    }

    public static ICarSpecs getDefaultNissanSpecs() {
        return new NissanCarSpecs();
    }

    public static ICarSpecs getDefaultToyotaSpecs() {
        return new ToyotaCarSpecs();
    }

    static ICarSpecs getCarSpecs(String carModel) {
        if (carModel.toLowerCase().contains("chevrolet"))
            return getDefaultChevroletSpecs();
        
        if (carModel.toLowerCase().contains("toyota"))
            return getDefaultToyotaSpecs();
        
        if (carModel.toLowerCase().contains("nissan"))
            return getDefaultNissanSpecs();
        
        return null;
    }
}
